package org.ly817.falsework.cloud.admin.service;

import org.ly817.falsework.cloud.admin.biz.UserBiz;

import org.ly817.falsework.cloud.admin.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * @author LuoYu
 * @date 2018/09/15 17:11
 * <p>
 * Description:
 * 用户服务 微服务接口
 */
@RestController
public class UserService {
    @Autowired
    private UserBiz userBiz;

    /**
     * 新增用户
     * @return
     */
    @PostMapping("/user")
    public Long addUser(@RequestBody User user) throws Exception {
        User existUser = userBiz.getUserByUsername(user.getUserName());
        if (existUser ==null){
            user.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            return (long) userBiz.getUserMapper().addNewUser(user);
        } else {
            throw new Exception("用户名重复");
        }
    }

    @GetMapping("/user/{id}")
    public User qryUserById(@PathVariable Long id){
        return userBiz.getUserById(id);
    }
}
